package composantes;

public class Cellule {
	private int _etat,
				_prochainEtat,
				_type,
				_x,
				_y;
	
	public Cellule(){
		_etat = _prochainEtat = _type = _x = _y = 0;
	}
	
	public Cellule(int etat, int type, int x, int y){
		_etat = _prochainEtat = etat;
		_type = type;
		_x = x;
		_y = y;
	}
	
	public Cellule(int x, int y){
		_x = x;
		_y = y;
	}
	
	public int getEtat(){
		return _etat;
	}
	
	public int getProchainEtat(){
		return _prochainEtat;
	}
	
	public int getType(){
		return _type;
	}
	
	public int getX(){
		return _x;
	}
	
	public int getY(){
		return _y;
	}
	
	public void setEtat(int etat) throws Exception{
		if(etat < 0 || etat > 2)
			throw new Exception("Exception d'etat invalide");
		_etat = _prochainEtat = etat;
	}
	
	public void setProchainEtat(int prochainEtat) throws Exception{
		if(prochainEtat < 0 || prochainEtat > 2)
			throw new Exception("Exception d'etat invalide");
		_prochainEtat = prochainEtat;
	}
	
	public void setPosition(int x, int y) throws Exception{
		if(x < 0 || y < 0)
			throw new Exception("Exception de position negative");
		_x = x;
		_y = y;
	}
	
	public void setType(int type){
		_type = type;
	}
	
	public void init(int etat, int type){
		_etat = _prochainEtat = etat;
		_type = type;
	}
	
	public boolean estVoisin(Cellule c){
		if(_x == c.getX()){
			if(_y == c.getY()+1 || _y == c.getY()-1)
				return true;
		}else if(_y == c.getY()){
			if(_x == c.getX()+1 || _x == c.getX()-1)
				return true;
		}
		return false;
			
	}
	
	public boolean peutGagnerEtat(){
		return _etat < 2;
	}
	
	public boolean peutPerdreEtat(){
		return _etat > 0;
	}
	
	public void gagnerEtat(){
		try{
			if(peutGagnerEtat())
				setProchainEtat(_etat+1);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	public void perdreEtat(){
		try{
			if(peutPerdreEtat())
				setProchainEtat(_etat-1);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	public void vieillir(){
		_etat = _prochainEtat;
	}
}
